import {Restivus} from 'meteor/nimble:restivus';
import {Meteor} from 'meteor/meteor';
import { Accounts } from 'meteor/accounts-base';
import { FS } from 'meteor/cfs:base-package';
// const situUrl = '119.254.97.31';
// const situUrl = '192.168.89.35';
const situUrl = 'scene.miitzc.com';
if (Meteor.isServer) {
    Meteor.startup(() => {
        let Api = new Restivus({
            useDefaultAuth: true,
            prettyJson: true,
        });
        // 添加情景
        Api.addRoute('addFamilySituation', {authRequired: false}, {
            post: {
                action() {
                    if (this.bodyParams) {
                        const param = this.bodyParams;
                        console.log('param : ', param);
                        const token = param.token;
                        const user = Meteor.users.findOne({"services.resume.loginTokens.token":token});
                        if(user) {
                            try {
                                const result = HTTP.call('POST', `http://${situUrl}/addSituation`, {
                                    data: { name: param.name || '', logo: param.logo || '', belongto: param.familyId || '', task: param.task || [] }
                                });
                                const content = JSON.parse(result.content);
                                console.log('content : ', content);
                                if (content.success === true) {
                                    return {success: true, result: '添加情景请求成功', resultCode: 1000};
                                }else{
                                    return  {success: false, result: content.msg || '添加情景请求失败！', resultCode: 1004};
                                }
                            }catch (e){
                                console.log(e);
                                return  {success: false, result: '添加情景请求失败！', resultCode: 1004};
                            }

                        }else{
                            return {success: false, result: '未获取到登录用户的信息！', resultCode: 1002};
                        }
                    }
                    return {success: false, result: '没有填写信息！', resultCode: 1002};

                }
            }
        });
        // 触发情景
        Api.addRoute('touchFamilySituation', {authRequired: false}, {
            post: {
                action() {
                    if (this.bodyParams) {
                        const param = this.bodyParams;
                        const token = param.token;
                        const user = Meteor.users.findOne({"services.resume.loginTokens.token":token});
                        if(user) {
                            try {
                                const result = HTTP.call('POST', `http://${situUrl}/touchSituation/${param.id}`, {
                                });
                                const content = JSON.parse(result.content);
                                if (content.success === true) {
                                    return {success: true, result: '触发情景请求成功', resultCode: 1000};
                                }else{
                                    return  {success: false, result: '触发情景请求失败！', resultCode: 1004};
                                }
                            }catch (e){
                                console.log('---- ', e);
                                return  {success: false, result: '触发情景请求失败！', resultCode: 1004};
                            }

                        }else{
                            return {success: false, result: '未获取到登录用户的信息！', resultCode: 1002};
                        }
                    }
                    return {success: false, result: '没有填写信息！', resultCode: 1002};

                }
            }
        });
        // 添加联动
        Api.addRoute('addFamilyLinkage', {authRequired: false}, {
            post: {
                action() {
                    if (this.bodyParams) {
                        const param = this.bodyParams;
                        const token = param.token;
                        console.log('param : ', param);
                        const user = Meteor.users.findOne({"services.resume.loginTokens.token":token});
                        if(user) {
                            try {
                                const result = HTTP.call('POST', `http://${situUrl}/addLinkage`, {
                                    data: { name: param.name || '', logo: param.logo || '', belongto: param.familyId || '', task: param.task || [], condition: param.condition }
                                });
                                const content = JSON.parse(result.content);
                                if (content.success === true) {
                                    return {success: true, result: '添加联动请求成功', resultCode: 1000};
                                }else{
                                    return  {success: false, result: content.msg || '添加联动请求失败！', resultCode: 1004};
                                }
                            }catch (e){
                                console.log(e);
                                return  {success: false, result: '添加联动请求失败！', resultCode: 1004};
                            }

                        }else{
                            return {success: false, result: '未获取到登录用户的信息！', resultCode: 1002};
                        }
                    }
                    return {success: false, result: '没有填写信息！', resultCode: 1002};

                }
            }
        });
        // 编辑情景
        Api.addRoute('editFamilySituation', {authRequired: false}, {
            post: {
                action() {
                    if (this.bodyParams) {
                        const param = this.bodyParams;
                        const token = param.token;
                        const name = param.name;
                        const logo = param.logo;
                        const task = param.task;
                        const user = Meteor.users.findOne({"services.resume.loginTokens.token":token});
                        if(user) {
                            try{
                                const result = HTTP.call('POST', `http://${situUrl}/editSituation/${param.id}`, {
                                    data: { name: name, logo: logo, task: task || [] }
                                });
                                const content = JSON.parse(result.content);
                                if (content.success === true) {
                                    return {success: true, result: '编辑情景请求成功', resultCode: 1000};
                                }else{
                                    return  {success: false, result: content.msg || '编辑情景请求失败！', resultCode: 1004};
                                }
                            }catch (e){
                                return  {success: false, result: '编辑情景请求失败！', resultCode: 1004};
                            }

                               return {success: false, result: '未获取到登录用户的信息！', resultCode: 1002};
                        }
                    }
                    return {success: false, result: '没有填写信息！', resultCode: 1002};
                }
            }
        });
        // 删除情景
        Api.addRoute('deleteFamilySituation', {authRequired: false}, {
            post: {
                action() {
                    if (this.bodyParams) {
                        const param = this.bodyParams;
                        const token = param.token;
                        console.log('param : ', param);
                        const user = Meteor.users.findOne({"services.resume.loginTokens.token":token});
                        if(user) {
                            try{
                                const result = HTTP.call('POST', `http://${situUrl}/delSituation/${param.id}`, {
                                });
                                const content = JSON.parse(result.content);
                                if (content.success === true) {
                                    return  {success: true, result: '删除情景请求成功', resultCode: 1000};
                                }else{
                                    return  {success: false, result: '删除情景请求失败！', resultCode: 1004};
                                }

                            }catch(e){
                                return  {success: false, result: '删除情景请求失败！', resultCode: 1004};
                            }
                        }else{
                            return {success: false, result: '未获取到登录用户的信息！', resultCode: 1002};
                        }
                    }
                    return {success: false, result: '没有填写信息！', resultCode: 1002};
                }
            }
        });
        // 查看情景
        Api.addRoute('getFamilySituation', {authRequired: false}, {
            get: {
                action() {
                    if (this.bodyParams) {
                        const param = this.bodyParams;
                        const token = param.token;
                        const user = Meteor.users.findOne({"services.resume.loginTokens.token":token});
                        if(user) {
                            try{
                                const result = HTTP.call('POST', `http://${situUrl}/getSituation/${param.id}`, {});
                                const content = JSON.parse(result.content);
                                if (content.success === true) {
                                    return  {success: true, result: {situation: content.data, error: ''}, resultCode: 1000};
                                }else{
                                    return  {success: false, result: {error: '查看情景请求失败！'}, resultCode: 1004};
                                }
                            }catch(e){
                                return  {success: false, result: {error: '查看情景请求失败！'}, resultCode: 1004};
                            }
                            
                        }else{
                            return {success: false, result: {error: '未获取到登录用户的信息！'}, resultCode: 1002};
                        }
                    }
                    return {success: false, result: {error: '没有填写信息！'}, resultCode: 1002};
                }
            }
        });
        // 添加任务
        Api.addRoute('addTask', {authRequired: false}, {
            get: {
                action() {
                    if (this.bodyParams) {
                        const param = this.bodyParams;
                        const token = param.token;
                        const user = Meteor.users.findOne({"services.resume.loginTokens.token":token});
                        if(user) {
                            try{
                                const result = HTTP.call('POST', `http://${situUrl}/addSitTask/${param.id}`, {
                                    params: {task: {device_name: param.device_name || '', device_number: param.device_number || '', deplay: param.deplay || '', action: param.action || ''}}
                                });
                                const content = JSON.parse(result.content);
                                if (content.success === true) {
                                    return  {success: true, result: '添加情景任务成功', resultCode: 1000};
                                }else{
                                    return  {success: false, result: '添加情景任务请求失败！', resultCode: 1004};
                                }
                            }catch(e){
                                return  {success: false, result: '添加情景任务请求失败！', resultCode: 1004};
                            }

                        }else{
                            return {success: false, result: '未获取到登录用户的信息！', resultCode: 1002};
                        }
                    }
                    return {success: false, result: '没有填写信息！', resultCode: 1002};
                }
            }
        });
        // 删除任务
        Api.addRoute('deleteTask', {authRequired: false}, {
            post: {
                action() {
                    if (this.bodyParams) {
                        const param = this.bodyParams;
                        const token = param.token;
                        const devId = param.devId;
                        const channel = param.channel;
                        const id = param.id;
                        const user = Meteor.users.findOne({"services.resume.loginTokens.token":token});
                        if(user) {
                            try {
                                const result = HTTP.call('POST', `http://${situUrl}/delSitTask/${id}`, {
                                    data: {devId: devId, channel: channel}
                                });
                                const content = JSON.parse(result.content);
                                if (content.success === true) {
                                    return  {success: true, result: '删除情景任务成功', resultCode: 1000};
                                }else{
                                    return  {success: false, result: '删除情景任务失败！', resultCode: 1004};
                                }
                            }catch (e){
                                return  {success: false, result: '删除情景任务失败！', resultCode: 1004};
                            }
                        }else{
                            return {success: false, result: {error: '未获取到登录用户的信息！'}, resultCode: 1002};
                        }
                    }
                    return {success: false, result: {error: '没有填写信息！'}, resultCode: 1002};
                }
            }
        });
        // 根据家庭ＩＤ查询情景
        Api.addRoute('situationByFamily', {authRequired: false}, {
            post: {
                action() {
                    if (this.bodyParams) {
                        const param = this.bodyParams;
                        console.log('param : ', param);
                        const token = param.token;
                        const user = Meteor.users.findOne({"services.resume.loginTokens.token":token});
                        const familyId = param.id;
                        if(user) {
                            try {
                                const result = HTTP.call('GET', `http://${situUrl}/getSituation/${familyId}`);
                                console.log('result : ', result);
                                const content = JSON.parse(result.content);
                                if (content.success === true) {
                                    return { success: true, result: {situations: content.data, error: ''}, resultCode: 1000};
                                }else{
                                    return  {success: false, result: {error: '查询情景请求失败！'}, resultCode: 1004};
                                }
                            }catch (e){
                                return {success: false, result: {error: '查询情景请求失败!'}, resultCode: 1004};
                            }
                        }else{
                            return {success: false, result: {error: '未获取到登录用户的信息！'}, resultCode: 1002};
                        }
                    }else {
                        return {success: false, result: {error: '没有填写信息！'}, resultCode: 1002};
                    }
                }
            }
        });
        // 根据家庭ＩＤ查询子联动
        Api.addRoute('linkAgeByFamily', {authRequired: false}, {
            post: {
                action() {
                    if (this.bodyParams) {
                        const param = this.bodyParams;
                        const token = param.token;
                        const user = Meteor.users.findOne({"services.resume.loginTokens.token":token});
                        const familyId = param.id;
                        if(user) {
                            try {
                                const result = HTTP.call('GET', `http://${situUrl}/getLinkage/${familyId}`);
                                console.log('result : ', result);
                                const content = JSON.parse(result.content);
                                if (content.success === true) {
                                    return { success: true, result: {linkages: content.data, error: ''}, resultCode: 1000};
                                }else{
                                    return  {success: false, result: {error: '查询联动请求失败！'}, resultCode: 1004};
                                }
                            }catch (e){
                                return {success: false, result: {error: '查询情景请求失败!'}, resultCode: 1004};
                            }
                        }else{
                            return {success: false, result: {error: '未获取到登录用户的信息！'}, resultCode: 1002};
                        }
                    }else {
                        return {success: false, result: {error: '没有填写信息！'}, resultCode: 1002};
                    }
                }
            }
        });
        // 编辑联动
        Api.addRoute('editLinkAge', {authRequired: false}, {
            post: {
                action() {
                    if (this.bodyParams) {
                        const param = this.bodyParams;
                        const token = param.token;
                        const user = Meteor.users.findOne({"services.resume.loginTokens.token":token});
                        const condition = param.condition;
                        const id = param.id;
                        const name = param.name;
                        const task = param.task;
                        if(user) {
                            try {
                                const result = HTTP.call('POST', `http://${situUrl}/editLinkage/${id}`, {
                                    data: { condition: condition, name: name, task: task}
                                });
                                const content = JSON.parse(result.content);
                                if (content.success === true) {
                                    return { success: true, result: '编辑联动成功！', resultCode: 1000};
                                }else{
                                    return {success: false, result: '编辑联动失败！', resultCode: 1004};
                                }
                            }catch (e){
                                return {success: false, result: '编辑联动失败!', resultCode: 1004};
                            }
                        }else{
                            return {success: false, result: '未获取到登录用户的信息！', resultCode: 1002};
                        }
                    }else {
                        return {success: false, result: '没有填写信息！', resultCode: 1002};
                    }
                }
            }
        });
        // 编辑联动状态
        Api.addRoute('editLinkAgeStatus', {authRequired: false}, {
            post: {
                action() {
                    if (this.bodyParams) {
                        const param = this.bodyParams;
                        const token = param.token;
                        const user = Meteor.users.findOne({"services.resume.loginTokens.token":token});
                        const id = param.id;
                        const status = param.status;
                        console.log('param : ', param);
                        if(user) {
                            try {
                                const result = HTTP.call('POST', `http://${situUrl}/updateLinkageStatus/${id}`, {
                                    data: { status: status}
                                });
                                const content = JSON.parse(result.content);
                                if (content.success === true) {
                                    return { success: true, result: '联动状态修改成功！', resultCode: 1000};
                                }else{
                                    return  {success: false, result: '联动状态修改失败！', resultCode: 1004};
                                }
                            }catch (e){
                                return {success: false, result: '联动状态修改失败!', resultCode: 1004};
                            }
                        }else{
                            return {success: false, result: '未获取到登录用户的信息！', resultCode: 1002};
                        }
                    }else {
                        return {success: false, result: '没有填写信息！', resultCode: 1002};
                    }
                }
            }
        });
        //修改联动的设备信息
        Api.addRoute('editLinkageDevList', {authRequired: false}, {
            post: {
                action() {
                    if (this.bodyParams) {
                        const param = this.bodyParams;
                        const token = param.token;
                        const user = Meteor.users.findOne({"services.resume.loginTokens.token":token});
                        const familyId = param.familyId;
                        const linkageId = param.linkageId;
                        if(user) {
                            try {
                                const result = HTTP.call('POST', `http://${situUrl}/linkageDevList`, {
                                    data: { familyId: familyId, linkageId: linkageId}
                                });
                                const content = JSON.parse(result.content);
                                if (content.success === true) {
                                    return { success: true, result: {devices: content.msg, error:''}, resultCode: 1000};
                                }else{
                                    return  {success: false, result: {error: '联动设备修改失败！'}, resultCode: 1004};
                                }
                            }catch (e){
                                return {success: false, result: {error: '联动设备修改失败！'}, resultCode: 1004};
                            }
                        }else{
                            return {success: false, result: {error: '未获取到登录用户的信息！'}, resultCode: 1002};
                        }
                    }else {
                        return {success: false, result: {error: '没有填写信息！'}, resultCode: 1002};
                    }
                }
            }
        });
        //修改联动的情景列表
        Api.addRoute('editLinkageSceneList', {authRequired: false}, {
            post: {
                action() {
                    if (this.bodyParams) {
                        const param = this.bodyParams;
                        const token = param.token;
                        const user = Meteor.users.findOne({"services.resume.loginTokens.token":token});
                        const familyId = param.familyId;
                        const linkageId = param.linkageId;
                        if(user) {
                            try {
                                const result = HTTP.call('POST', `http://${situUrl}/linkageSceneList`, {
                                    data: { familyId: familyId, linkageId: linkageId}
                                });
                                const content = JSON.parse(result.content);
                                if (content.success === true) {
                                    return { success: true, result: {situations: content.msg, error: ''}, resultCode: 1000};
                                }else{
                                    return  {success: false, result: {error: '联动情景列表修改失败！'}, resultCode: 1004};
                                }
                            }catch (e){
                                return {success: false, result: {error: '联动情景列表修改失败!'}, resultCode: 1004};
                            }
                        }else{
                            return {success: false, result: {error: '未获取到登录用户的信息！'}, resultCode: 1002};
                        }
                    }else {
                        return {success: false, result: {error: '没有填写信息！'}, resultCode: 1002};
                    }
                }
            }
        });
        //删除联动
        Api.addRoute('deleteLinkage', {authRequired: false}, {
            post: {
                action() {
                    if (this.bodyParams) {
                        const param = this.bodyParams;
                        const token = param.token;
                        const user = Meteor.users.findOne({"services.resume.loginTokens.token":token});
                        const id = param.id;
                        if(user) {
                            try {
                                const result = HTTP.call('POST', `http://${situUrl}/delLinkage/${id}`);
                                const content = JSON.parse(result.content);
                                if (content.success === true) {
                                    return { success: true, result: '联动删除成功！', resultCode: 1000};
                                }else{
                                    return  {success: false, result: '联动删除失败！', resultCode: 1004};
                                }
                            }catch (e){
                                return {success: false, result: '联动删除失败!', resultCode: 1004};
                            }
                        }else{
                            return {success: false, result: '未获取到登录用户的信息！', resultCode: 1002};
                        }
                    }else {
                        return {success: false, result: '没有填写信息！', resultCode: 1002};
                    }
                }
            }
        });
        // 编辑情景时查询还没有添加的设备。
        Api.addRoute('getDevicesByEditSituation', {authRequired: false}, {
            post: {
                action() {
                    if (this.bodyParams) {
                        const param = this.bodyParams;
                        const token = param.token;
                        const user = Meteor.users.findOne({"services.resume.loginTokens.token":token});
                        const familyId = param.familyId;
                        const situationId = param.situationId;
                        if(user) {
                            try {
                                const result = HTTP.call('POST', `http://${situUrl}/situationDevList`, {
                                    data: { familyId: familyId, situationId: situationId}
                                });
                                const content = JSON.parse(result.content);
                                if (content.success === true) {
                                    return {success: true, result: {devices: content.msg, error: ''}, resultCode: 1000};
                                }else{
                                    return  {success: false, result:{error: '请求失败！'}, resultCode: 1004};
                                }
                            }catch (e){
                                return {success: false, result: {error: '请求失败!'}, resultCode: 1004};
                            }
                        }else{
                            return {success: false, result: {error: '未获取到登录用户的信息！'}, resultCode: 1002};
                        }
                    }else {
                        return {success: false, result: {error: '没有填写信息！'}, resultCode: 1002};
                    }
                }
            }
        });
    });
}